<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    let arr = [
        {a: 5, b: 10, c: 20, msg: 'success'},
        {a: 5, b: 10, c: 20, msg: 'warn'},
        {a: 100, b: 100, c: 100, msg: 'error'}
    ];


    console.log('数组:', arr)

    let sum = {}

    // 获取求和数组的属性，并给属性设置初始值为0
    Object.keys(arr[0]).forEach((item) => {
        Object.defineProperty(sum, item, {
            value: 0,
            writable: true,
            enumerable: true,
            configurable: true
        })
    })

    console.log('before:', sum)  //  { a:0,  b:0,  c:0,  msg:0 }

    for (let pos in sum) {
        console.log("pos:", pos)
        let total = arr.reduce((total, row) => {
            console.log("row:", row)
            return total + parseInt(row[pos])
        }, 0)
        sum[pos] = !isNaN(total) ? total : '-'
    }

    console.log('after:', sum)

</script>
</body>
</html>